package com.innovation.chengzhangriji.ui.adapter;

import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.FrameLayout;
import android.widget.ProgressBar;
import android.widget.RelativeLayout;
import android.widget.TextView;
import com.innovation.chengzhangriji.R;

public class DTProgressHolder extends RecyclerView.ViewHolder implements View.OnClickListener {

    public interface OnClickErrorListener {

        void onClickError();

    }

    private OnClickErrorListener onClickErrorListener;

    private ProgressBar progressBar;

    private TextView textView;

    public DTProgressHolder(View itemView) {
        super(itemView);
        RelativeLayout layout = (RelativeLayout) itemView.findViewById(R.id.layout);
        layout.setOnClickListener(this);
        progressBar = (ProgressBar) itemView.findViewById(R.id.progress);
        textView = (TextView) itemView.findViewById(R.id.text);
    }

    @Override
    public void onClick(View v) {
        if (onClickErrorListener == null) {
            return;
        }
        switch (v.getId()) {
            case R.id.layout:
                onClickErrorListener.onClickError();
                break;
        }
    }

    public void setOnClickErrorListener(OnClickErrorListener onClickErrorListener) {
        this.onClickErrorListener = onClickErrorListener;
    }

    public void setError(boolean error) {
        if (error) {
            progressBar.setVisibility(View.INVISIBLE);
            textView.setText(R.string.load_failed);
        } else {
            progressBar.setVisibility(View.VISIBLE);
            textView.setText(R.string.loading);
        }
    }
}